package com.evan.review.eventdispatch;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.evan.review.R;

import java.util.ArrayList;
import java.util.List;

/**
 * ScrollView和RecyclerView滚动冲突演示Activity
 * 
 * 该Activity展示了常见的嵌套滚动冲突问题及解决方案：
 * 1. 未解决冲突：ScrollView嵌套RecyclerView时的滚动冲突
 * 2. 解决方案：使用NestedScrollView和RecyclerView.setNestedScrollingEnabled()
 */
public class ScrollConflictActivity extends AppCompatActivity {

    private Button btnShowConflict;
    private Button btnShowSolution;
    private View conflictLayout;
    private View solutionLayout;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scroll_conflict);
        
        setTitle("滚动冲突演示");
        
        initViews();
        setupRecyclerViews();
        
        // 默认显示冲突布局
        showConflictLayout();
    }
    
    private void initViews() {
        btnShowConflict = findViewById(R.id.btn_show_conflict);
        btnShowSolution = findViewById(R.id.btn_show_solution);
        conflictLayout = findViewById(R.id.layout_conflict);
        solutionLayout = findViewById(R.id.layout_solution);
        
        btnShowConflict.setOnClickListener(v -> showConflictLayout());
        btnShowSolution.setOnClickListener(v -> showSolutionLayout());
    }
    
    private void showConflictLayout() {
        conflictLayout.setVisibility(View.VISIBLE);
        solutionLayout.setVisibility(View.GONE);
        btnShowConflict.setEnabled(false);
        btnShowSolution.setEnabled(true);
        Toast.makeText(this, "冲突演示：内部列表无法流畅滚动", Toast.LENGTH_SHORT).show();
    }
    
    private void showSolutionLayout() {
        conflictLayout.setVisibility(View.GONE);
        solutionLayout.setVisibility(View.VISIBLE);
        btnShowConflict.setEnabled(true);
        btnShowSolution.setEnabled(false);
        Toast.makeText(this, "解决方案：使用NestedScrollView", Toast.LENGTH_SHORT).show();
    }
    
    private void setupRecyclerViews() {
        // 设置有冲突的RecyclerView
        RecyclerView rvConflict = findViewById(R.id.rv_conflict);
        rvConflict.setLayoutManager(new LinearLayoutManager(this));
        rvConflict.setAdapter(new SimpleAdapter(generateData()));
        
        // 设置解决方案中的RecyclerView
        RecyclerView rvSolution = findViewById(R.id.rv_solution);
        rvSolution.setLayoutManager(new LinearLayoutManager(this));
        rvSolution.setAdapter(new SimpleAdapter(generateData()));
        rvSolution.setNestedScrollingEnabled(true); // 这是解决方案的关键之一
    }
    
    private List<String> generateData() {
        List<String> data = new ArrayList<>();
        for (int i = 1; i <= 30; i++) {
            data.add("列表项 #" + i);
        }
        return data;
    }
    
    /**
     * 简单的RecyclerView适配器
     */
    private static class SimpleAdapter extends RecyclerView.Adapter<SimpleAdapter.ViewHolder> {
        
        private final List<String> data;
        
        public SimpleAdapter(List<String> data) {
            this.data = data;
        }
        
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_simple_list, parent, false);
            return new ViewHolder(view);
        }
        
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            holder.tvItem.setText(data.get(position));
        }
        
        @Override
        public int getItemCount() {
            return data.size();
        }
        
        static class ViewHolder extends RecyclerView.ViewHolder {
            final TextView tvItem;
            
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                tvItem = itemView.findViewById(R.id.tv_item);
            }
        }
    }
} 